import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'CosManager.dart';
import 'CosPlugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  Map<String, dynamic> uploadConfig = {
    "tmpSecretId":
        "AKIDWcbYVxHA-hsN5WGt2ghSMDVfYKkbUrMQTlXoYZ4h5tIepK14rvm8Hpj_VAMsbxhw",
    "tmpSecretKey": "I+Dyt/Lvbx3k58SmNVShAKiKUD18UKiHomQNb2CarJQ=",
    "sessionToken":
        "E3aqliEMjHAdK8FsubZthmzcnMsONrEE513378566d155ac93a3163e4241e340bdrjHQ5bIfz_8pII6yAiY_5Ls-1k-Mm7k6Si0z9dgnbymUCZbvsnsxWHkUvWuYazYNw6bx9CBCrDYV2-DFF1aX1sIc_ke2tdu2vr3keZTRZqPLgX74hZ9o_boXVeaZQR5Z6LzyLU-KmKzYRu-w6XiGx9tRMUdfZ5DXT1HrtVGxMZCJSANSfqVokvBoJ05CTayOkaLKtU3SGWeo0DwcWEq4Md0FN79APyZzd-y4hAWe_OUq1ci-JH7hLY-eKIg86temNV0gEHWCrJ3RZSNdZBfkH7Tnx4bha631rP2-iftiDsSLfHcYNtoz5Zij3wvTmlRdwm1SXKWcNFuViRZ5dJ8gZ1KyDZ8bJDyfK30aYx3PZM3mEesEVn7wiTLjUNdyJ27o_39N3E2zxhPuptHaAxyyL4s9nuNNCl72S-4jE51FRUINjsf6oxNHJ4XupolAKnBOtEKbJjGYOoyviT1FAqZCHsST1vGNIIpj907L47VU7ljWldL1qMmz8VmW-eQHuSvNCng2HAPOY1ccONv9CorGqrpyDcOx0h2rzynVTrY44M_6G_T9b29NdYZcKH6SQJi",
    "startTime": "1598202578",
    "expiredTime": "1598204378",
    "region": "ap-shanghai",
  };

  //过期回调
  void getTokenCallBack() {
    showMessage("Cos认证凭证过期了");
  }

  void _incrementCounter() {
    showMessage("开始上传文件");
    var cosManager = new CosManager();
    cosManager.init(uploadConfig, getTokenCallBack);
    var simpleUploadFile = cosManager.simpleUploadFile("circuit-1300618745",
        "/sdcard/DCIM/Camera/1111111111.png", "/aaaa.png");
    showMessage("结束上传文件-->$simpleUploadFile");

//    var object = cosManager.getObject("circuit-1300618745", "/aaaa.png");
//    var file = new File("/sdcard/DCIM/Camera/2.png");

//    object.asStream().transform(
//          new StreamTransformer.fromHandlers(
//            handleData: (data, sink) {
//              print(data);
//            },
//            handleError: (error, stack, sink) {},
//            handleDone: (sink) {
//              sink.close();
//            },
//          ),
//        );

//    var cosPlugin = new CosPlugin();
//    cosPlugin.launch("测试一下子", (str) => {showMessage("收到回到结果了:-->" + str)});
//
//    cosPlugin.uploadFile(
//        uploadConfig, (str) => {showMessage("收到回到结果了:-->" + str)});
  }

  void showMessage(String msg) {
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black87,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),

        // This trailing comma makes auto-formatting nicer for build methods.
        );
  }
}
